package com.gtc.zsk.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gtc.zsk.common.BaseResponse;
import com.gtc.zsk.common.ErrorCode;
import com.gtc.zsk.common.ResultUtils;
import com.gtc.zsk.constant.UserConstant;
import com.gtc.zsk.exception.BusinessException;
import com.gtc.zsk.model.dto.managepwd.UpdateManagePwdRequest;
import com.gtc.zsk.model.dto.managepwd.ValidManagePwdRequest;
import com.gtc.zsk.model.entity.ManagePwd;
import com.gtc.zsk.service.ManagePwdService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/managepwd")
public class ManagePwdController {

    @Resource
    ManagePwdService managePwdService;

    private static final String SALT = "gongtianchi";

    @PostMapping("/valid")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> validManagePwd(@RequestBody ValidManagePwdRequest validManagePwdRequest) {
        Integer id = validManagePwdRequest.getId();
        if (ObjectUtils.anyNull(validManagePwdRequest, id) || id <= 0)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        String pwd = validManagePwdRequest.getPwd();
        if (StringUtils.isBlank(pwd))
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码不能为空!");
        if (pwd.length() != 6)
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码输入长度不合法!");
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + pwd).getBytes());
        QueryWrapper<ManagePwd> qw = new QueryWrapper<>();
        qw.eq("id", id);
        qw.eq("pwd", encryptPassword);
        ManagePwd pwdEntity = managePwdService.getOne(qw);
        if (pwdEntity == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误,操作失败!");
        return ResultUtils.success(pwdEntity.getId());
    }

    @PostMapping("/update")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> updateManagePwd(@RequestBody UpdateManagePwdRequest updateManagePwdRequest) {
        Long id = updateManagePwdRequest.getId();
        if (ObjectUtils.anyNull(updateManagePwdRequest, id) || id <= 0)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        String oldPwd = updateManagePwdRequest.getOldPwd();
        String newPwd = updateManagePwdRequest.getNewPwd();
        String checkNewPwd = updateManagePwdRequest.getCheckNewPwd();
        if (StringUtils.isAnyBlank(oldPwd, newPwd, checkNewPwd))
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "原密码、新密码、确认密码都不能为空!");
        if (oldPwd.length() != 6 || newPwd.length() != 6 || checkNewPwd.length() != 6)
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码输入长度不合法!");
        if (!newPwd.equals(checkNewPwd))
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次新密码输入不一致!");

        String encryptOldPassword = DigestUtils.md5DigestAsHex((SALT + oldPwd).getBytes());
        QueryWrapper<ManagePwd> qw = new QueryWrapper<>();
        qw.eq("id", id);
        qw.eq("pwd", encryptOldPassword);
        ManagePwd pwdEntity = managePwdService.getOne(qw);
        if (pwdEntity == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "原密码输入错误,再想一想!");

        ManagePwd managePwd = new ManagePwd();
        String encryptNewPassword = DigestUtils.md5DigestAsHex((SALT + newPwd).getBytes());
        managePwd.setPwd(encryptNewPassword);
        managePwd.setId(id);
        boolean result = managePwdService.updateById(managePwd);
        if (!result)
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "数据库内部异常,修改失败");
        return ResultUtils.success(managePwd.getId());
    }

}
